#ifndef _EB0_RENDER_WINDOW_H_
#define _EB0_RENDER_WINDOW_H_

#include <EB0/Prereq.h>
#include <SFML/Graphics.hpp>
#include <set>
#include <list>

namespace EB0
{
namespace Window
{
	class RenderWindow : public sf::RenderWindow
	{
	public:
		RenderWindow(int width, int height, char* title, bool resize = false);
		~RenderWindow();

		void handleEvents(); // handle all window events (e.g. keyboard, mouse, resizing)
		void update(float dt = 0.0f); // update states
		void render(float dt = 0.0f); // draw contents of viewports and overlays

		Managers::StateManager* getStateManager() const;

		void setClearColor(sf::Color color);
		void setClearColor(sf::Uint8 r, sf::Uint8 g, sf::Uint8 b, sf::Uint8 a = 255);
		sf::Color getClearColor();

		Layer* createLayer(unsigned int zindex = UINT_MAX);
		bool destroyLayer(Layer*& layer);
		void destroyAllLayers();
		int getNumLayers() const;
		unsigned short getMaxLayerZ();

		void attachWindowListener(EB0::Window::WindowListener* windowListener); // EB0::Window::WindowListener to avoid clashes
		void removeWindowListener(EB0::Window::WindowListener* windowListener); // with with sf::WindowListener
		void removeAllWindowListeners();

		void attachFrameListener(FrameListener* frameListener);
		void removeFrameListener(FrameListener* frameListener);
		void removeAllFrameListeners();

	private:
		typedef std::list<Layer*> LayerList;
		typedef std::set<Layer*> LayerSet;
		typedef std::set<EB0::Window::WindowListener*> WindowListenerSet;
		typedef std::set<FrameListener*> FrameListenerSet;

		sf::Color mClearColor;

		LayerList mLayers;
		WindowListenerSet mWindowListeners;
		FrameListenerSet mFrameListeners;

		bool mDirtyLayerList;
		bool mDirtyWindowListenerSet;
		bool mDirtyFrameListenerSet;

		Managers::StateManager* mStateManager;

		sf::Event mEvent; // for processing input events

		mutable sf::Mutex mMutex;
		static sf::Image* mWindowIcon;
		static int mWindowCount; // destroy the window icon if no more windows exist
	};
} // namespace Window
}

#endif // _EB0_RENDER_WINDOW_H_